import csv
import xml
import shutil


def parseXML(filePath):
    DOMTree = xml.dom.minidom.parse(filePath)
    collection = DOMTree.documentElement
    contents = collection.getElementsByTagName("p")
    txtPath = filePath.replace("outputXML", "outputTXT")
    txtPath = txtPath.replace('grobid.xml', 'txt')
    print(txtPath)
    with open(txtPath, 'a') as f:
        for content in contents:
            #print(type(content.childNodes[0].data))
            f.write(content.childNodes[0].data)
   

def pdf_parser():
    pdfParser = Parser('grobid')
    pdfParser.parse('text', './data/RawPDF', './data/outputXML')
    #pdfParser.parse('text', '/home/troykuo/Elasticsearch/a1484358507860fac5bd566d27532c795f844e4c.pdf','./data/outputXML',80)


def cp_rawPDF(info):
        #print(i)
    #if info.has_key('PDFPath'):
    if 'PDFPath' in info.keys():
        try:
            filepath=info['PDFPath']+'.pdf'
            shutil.copy(filepath, 'data/RawPDF')
            print("copy success!")
        except:
            print("Can't copy")
            with open('data/RawPDF/copyFailed.txt', 'a') as f:
                f.write(str(info))
                f.write('\n')


if __name__ == '__main__':
    print('finish')
